package com.macro.mall.portal.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.common.exception.Asserts;
import com.macro.mall.common.utils.Assist;
import com.macro.mall.mapper.BannerInfoMapper;
import com.macro.mall.model.BannerInfo;
import com.macro.mall.model.UmsMember;
import com.macro.mall.portal.domain.MemberDetails;
import com.macro.mall.portal.service.BannerInfoService;
import com.macro.mall.portal.service.UmsMemberService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.List;
/**
 * BannerInfo的服务接口的实现类
 * 
 * @author gaodx
 *
 */
@Slf4j
@Service
public class BannerInfoServiceImpl implements BannerInfoService {

    private boolean isDebugEnabled;

	@Autowired
	private BannerInfoMapper bannerInfoMapper;
	@Autowired
	private UmsMemberService umsMemberService;


	@Override
	public List<BannerInfo> find(String channel,  String sourceType,String position,String orderBy){
		//TODO这里可以做通过Assist做添加查询
        Assist assist =new Assist(Assist.whereRequire("and status = ", "1"));
        if(StrUtil.isNotEmpty(channel)) {
            assist.andEq("channel", channel);
        }
        if(StrUtil.isNotEmpty(position)) {
           assist.andEq("position", position);
        }
        if(StrUtil.isNotEmpty(sourceType)) {
           assist.andEq("source_type", sourceType);
        }

        //TODO  判断03 问题
        UmsMember currentMember = getCurrentMember();
        //用户类型 01-服务 02-学院类 03-服务、学院

        //01-课程 02-服务 03-新闻 04-广告
        if("02".equals(currentMember.getUserType())) {
            assist.andNeq("source_type", "02");
        }


        if (StrUtil.isNotEmpty(orderBy)) {
            PageHelper.orderBy(orderBy);
        }
		List<BannerInfo> result = bannerInfoMapper.selectBannerInfo(assist);
		if (isDebugEnabled) {
			log.debug("执行获取BannerInfo数据集-->结果:", result);
		}
		return result;
	}
	@Override
	public BannerInfo findOne(Integer id) {
		if (id == null) {
			if (isDebugEnabled) {
				log.debug("执行通过BannerInfo的id获得BannerInfo对象-->失败:id不能为空");
			}
            Asserts.fail("执行通过BannerInfo的id获得BannerInfo对象-->失败:id不能为空");
		}
		BannerInfo result = bannerInfoMapper.selectBannerInfoById(id);
		if (isDebugEnabled) {
			log.debug("执行通过BannerInfo的id获得BannerInfo对象-->结果:", result);
		}
		return result;
	}

	@Override
	public CommonResult saveNotNull(BannerInfo value) {
		if (value == null) {
			if (isDebugEnabled) {
				log.debug("执行将BannerInfo中属性值不为null的数据保存到数据库-->失败:对象不能为空");
			}
                Asserts.fail("失败:对象不能为空");
		}
		int result = bannerInfoMapper.insertNotNullBannerInfo(value);
		if (isDebugEnabled) {
			log.debug("执行将BannerInfo中属性值不为null的数据保存到数据库-->结果:", result);
		}
            return CommonResult.success(null);
	}
	@Override
	public CommonResult updateNotNullById(BannerInfo value) {
		if (value == null) {
			if (isDebugEnabled) {
				log.debug("执行通过BannerInfo的id更新BannerInfo中属性不为null的数据-->失败:对象为null");
			}
                Asserts.fail("执行通过BannerInfo的id更新BannerInfo中属性不为null的数据-->失败:对象为null");
		}
		int result = bannerInfoMapper.updateNotNullBannerInfoById(value);
		if (isDebugEnabled) {
			log.debug("执行通过BannerInfo的id更新BannerInfo中属性不为null的数据-->结果:", result);
		}
        return CommonResult.success(null);
	}

	@Override
	public CommonResult deleteById(Integer id) {
		if (id == null) {
			if (isDebugEnabled) {
				log.debug("执行通过BannerInfo的id删除BannerInfo-->失败:id不能为空");
			}
                Asserts.fail("执行通过BannerInfo的id删除BannerInfo-->失败:id不能为空");
		}
		int result = bannerInfoMapper.deleteBannerInfoById(id);
		if (isDebugEnabled) {
			log.debug("执行通过BannerInfo的id删除BannerInfo-->结果:", result);
		}
        return CommonResult.success(null);
	}

    public UmsMember getCurrentMember() {
        SecurityContext ctx = SecurityContextHolder.getContext();
        Authentication auth = ctx.getAuthentication();
        MemberDetails memberDetails = (MemberDetails) auth.getPrincipal();
        return umsMemberService.getById(memberDetails.getUmsMember().getId());
    }

}